/**
 * @filename:UserServiceImpl 2019年4月9日
 * @project user_center  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd. 
 * All right reserved. 
 */
package com.association.service.impl;

import com.association.constant.CacheConstant;
import com.association.constant.Global;
import com.association.mapper.DeptMapper;
import com.association.mapper.UserMapper;
import com.association.mapper.WebProjectMapper;
import com.association.model.entity.Dept;
import com.association.model.entity.User;
import com.association.model.vo.CourierTokenBean;
import com.association.model.vo.UserVo;
import com.association.service.LoginService;
import com.association.service.UserService;
import com.association.utils.Md5Util;
import com.association.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.len.base.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**   
 *  
 * @Description:  用户——SERVICEIMPL
 * @Author:       BianPeng   
 * @CreateDate:   2019年4月9日
 * @Version:      V1.0
 *    
 */
@Service
public class LoginServiceImpl extends BaseServiceImpl<User, String> implements LoginService {

	@Resource
	DeptMapper deptMapper;
	@Resource
	RedisTemplate redisTemplate;

	@Autowired
	RedisUtil redisUtil;

	@Override
	public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response, HttpSession session, UserVo userVo) {
		//查询用户
		QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
		userQueryWrapper.eq("userName", userVo.getUserName());
		userQueryWrapper.eq("password", Md5Util.md5Encode(userVo.getPassword())).eq("enabled", 0);

		User user = this.getOne(userQueryWrapper);
		Map<String, Object> map = new HashMap<>();
		if (user != null) {
			user.setDeptName(deptMapper.selectById(user.getDeptId()).getName());
			user.setProjectName(deptMapper.selectById(user.getProjectId()).getName());
			user.setRoleName(deptMapper.selectById(user.getRoleId()).getName());
			//获取登录 token
			String token = session.getId();
			redisUtil.setex(String.format(CacheConstant.TOKEN_KEY_PREFIX,token),getCourierTokenBean(user), Global.ONE_DAY);
			//设置 token 至 cookie
			map.put("userInfo", user);
			map.put("token", token);
			//修改用户登录的时间
			User userInfo = new User();
			userInfo.setId(user.getId());
			userInfo.setUpdateTime(new Date());
			userInfo.setLastIp(request.getRemoteAddr());
			this.updateById(userInfo);
		}
		return map;
	}

	public static CourierTokenBean getCourierTokenBean(User user){
		return CourierTokenBean.builder()
				.userCode(user.getId().toString())
				.userName(user.getUserName())
				.phone(user.getPhone())
				.projectCode(user.getProjectId().toString())
				.deptId(user.getDeptId().toString())
				.jobId(user.getRoleId().toString())
				.build();
	}



}